package cn.com.anypay.manager.miaoma.manual.student;

import cn.com.anypay.manager.common.controller.BaseController;
import cn.com.anypay.manager.common.enums.ResultCode;
import cn.com.anypay.manager.common.response.ApiResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/anon/student")
public class StudentController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private StudentService studentService;

    // 获取Student列表（支持分页和条件查询）
    @GetMapping
    public ApiResponse<Page<Student>> list(@RequestParam(required = false, defaultValue = "1") int pageNumber
                                   , @RequestParam(required = false, defaultValue = "10") int pageSize
                                   , @RequestParam(required = false) String name
                                   ) {
        log.info("=== Student 分页请求开始 ===");
        log.info("请求参数: pageNumber={}, pageSize={}, name={}", pageNumber, pageSize, name);

        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();

        // 只有当参数不为空且不为空字符串时才添加条件

        Page<Student> newPage = new Page<>(pageNumber, pageSize);
        Page<Student> resultPage = (Page<Student>) studentService.selectPage(newPage, queryWrapper);

        log.info("分页查询结果: total={}, current={}, size={}, records.size={}",
                 resultPage.getTotal(), resultPage.getCurrent(),
                 resultPage.getSize(), resultPage.getRecords().size());
        log.info("=== Student 分页请求结束 ===");

        return success(resultPage);
    }

    // 添加新Student
    @PostMapping
    public ApiResponse<Void> add(@RequestBody Student record) {
        // 设置必要的默认值
        if (record.getId() == null) {
            record.setId(0L); // 默认Id
        }
        if (record.getStudentNo() == null) {
            record.setStudentNo(""); // 默认StudentNo
        }
        if (record.getName() == null) {
            record.setName(""); // 默认Name
        }
        if (record.getGender() == null) {
            record.setGender(0); // 默认Gender
        }
        if (record.getAge() == null) {
            record.setAge(0); // 默认Age
        }
        if (record.getBirthDate() == null) {
            record.setBirthDate(LocalDate.now()); // 默认BirthDate
        }
        if (record.getClassName() == null) {
            record.setClassName(""); // 默认ClassName
        }
        if (record.getEnrollDate() == null) {
            record.setEnrollDate(LocalDate.now()); // 默认EnrollDate
        }
        if (record.getStatus() == null) {
            record.setStatus(0); // 默认Status
        }

        studentService.insert(record);
        return success();
    }

    // 获取Student详情
    @GetMapping("/{recordId}")
    public ApiResponse<Student> detail(@PathVariable Long recordId) {
        Student record = studentService.selectById(recordId);
        if (record == null) {
            return fail(ResultCode.CUSTOM_FAIL, "Student not found");
        }
        return success(record);
    }

    // 更新Student信息
    @PutMapping("/{recordId}")
    public ApiResponse<Void> update(@PathVariable Long recordId, @RequestBody Student record) {
        record.setId(recordId);
        studentService.updateById(record);
        return success();
    }

    // 删除Student
    @DeleteMapping("/{recordId}")
    public ApiResponse<Void> delete(@PathVariable Long recordId) {
        studentService.deleteById(recordId);
        return success();
    }

    // 批量删除Student
    @DeleteMapping
    public ApiResponse<Void> deleteBatch(@RequestBody List<Long> ids) {
        studentService.deleteBatchIds(ids);
        return success();
    }

    // 根据条件查询Student
    @PostMapping("/search")
    public ApiResponse<List<Student>> search(@RequestBody Map<String, Object> queryMap) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(queryMap);
        List<Student> list = studentService.selectList(queryWrapper);
        return success(list);
    }

    // 获取Student数量
    @GetMapping("/count")
    public ApiResponse<Long> count(@RequestParam Map<String, Object> queryMap) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(queryMap);
        Long count = studentService.selectCount(queryWrapper);
        return success(count);
    }

    // 获取Student的 Map 数据
    @GetMapping("/maps")
    public ApiResponse<List<Map<String, Object>>> listMaps(@RequestParam Map<String, Object> queryMap) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(queryMap);
        List<Map<String, Object>> maps = studentService.selectMaps(queryWrapper);
        return success(maps);
    }

    // 分页查询Student的 Map 数据
    @GetMapping("/mapsPage")
    public ApiResponse<Page<Map<String, Object>>> listMapsPage(@RequestParam int page,
                                                          @RequestParam int size,
                                                          @RequestParam Map<String, Object> queryMap) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(queryMap);
        Page<Map<String, Object>> mapPage = new Page<>(page, size);
        Page<Map<String, Object>> resultPage = (Page<Map<String, Object>>) studentService.selectMapsPage(mapPage, queryWrapper);
        return success(resultPage);
    }
}